﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace E.Y.E.Jobs
{
    class CommandSearch : ICommand
    {
        private const string MESSAGE_COMMAND_TITLE = "/=======SEARCH COMMAND=======\\";
        private const string MESSAGE_SUCCESSFUL_EDIT = "The search command is sucessful.";
        private const string ERROR_MESSAGE_FAIL_EDITING = "So embarrasing. Error occured while we are trying to search tasks for you.";

        ArrayList OriginalDatabase;
        ArrayList SearchResult;
        ArrayList Criteria;
        String Message;
        bool ErrorFlag;

        public CommandSearch()
        {
            OriginalDatabase = new ArrayList();
            SearchResult = new ArrayList();
            Criteria = new ArrayList();
            Message = "";
            ErrorFlag = true;
        }

        public bool Execute(ref TaskManager taskDatabase, ref CommandTypeManager CommandTypeDatabase, ref History actionsHistory)
        {
            bool EmptySearching = Criteria.Count == 0;
            if (EmptySearching)
            {
                Message = ERROR_MESSAGE_FAIL_EDITING;
                ErrorFlag = true;
                return false;
            }

            try
            {
                SearchResult = taskDatabase.Search(Criteria);

                Message = MESSAGE_SUCCESSFUL_EDIT;
                ErrorFlag = false;
                return true;
            }
            catch
            {
                Message = ERROR_MESSAGE_FAIL_EDITING;
                ErrorFlag = true;
                return false;
            }
        }

        public bool Undo(ref TaskManager taskDatabase, ref CommandTypeManager CommandTypeDatabase, ref History actionsHistory)
        {
            // NOT UNDO operation for SEARCH command
            return true;
        }

        public string getCommandEventTitle()
        {
            return MESSAGE_COMMAND_TITLE;
        }

        public void setMessage(string desiredMessage)
        {
            Message = desiredMessage;
        }

        public string getMessage()
        {
            return Message;
        }

        public void SetError(bool desiredBool)
        {
            ErrorFlag = desiredBool;
        }

        public bool GetErrorState()
        {
            return ErrorFlag;
        }

        public InformationPackage ConvertToInfoPackage()
        {
            InformationPackage Result = new InformationPackage();

            if (ErrorFlag)
            {
                Result.CommandType = InformationPackageEnumClass.CommandType.OperationError;
            }
            else
            {
                Result.CommandType = InformationPackageEnumClass.CommandType.Search;
                Result.TaskList = SearchResult;
            }
            Result.Message = Message;

            return Result;
        }

        public ArrayList SearchCriteria
        {
            get
            {
                return Criteria;
            }
            set
            {
                Criteria = value;
            }
        }
    }
}
